package com.sqk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqk.reggie.dto.SetmealDto;
import com.sqk.reggie.mapper.SetmealMapper;
import com.sqk.reggie.pojo.Setmeal;
import com.sqk.reggie.pojo.SetmealDish;
import com.sqk.reggie.service.SetmealDishService;
import com.sqk.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 保存套餐，同时保存与套餐关联的菜品
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);

        //获得SetmealDish数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //Setmeal的Dish数据需要关联Setmeal的id
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存与套餐关联的菜品信息
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时删除套餐对应的菜品
     * @param ids
     */
    @Override
    @Transactional
    public void removeMealWithDish(String ids) {
        //拆分id
        String[] setmealIds = ids.split(",");

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //遍历setmealIds
        Arrays.stream(setmealIds).forEach(item -> {
            long setmealId = Long.parseLong(item);

            //删除套餐
            this.removeById(setmealId);

            //删除套餐对应的菜品
            queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
            setmealDishService.remove(queryWrapper);
        });
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getMealById(Long id) {

        SetmealDto setmealDto = new SetmealDto();

        //根据id查询Setmeal
        Setmeal setmeal = this.getById(id);

        //根据setmealId获取dish
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> dishList = setmealDishService.list(queryWrapper);

        //将meal，与相关联的dish封装到SetmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(dishList);

        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateMealWithDish(SetmealDto setmealDto) {
        //修改套餐的基本信息
        this.updateById(setmealDto);

        Long mealId = setmealDto.getId();

        //修改套餐对应的菜品，(先删除在添加)
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,mealId);
        setmealDishService.remove(queryWrapper);

        //保存菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            //前端发送的元数据中id可能有值，在添加时可能与数据库中的id重复
            item.setId(null);
            item.setSetmealId(mealId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
